package com.waypolice.manager.service.impl;

import com.waypolice.common.utils.MathUtils;
import com.waypolice.common.utils.StringUtil;
import com.waypolice.manager.service.CmsSinItemService;
import com.waypolice.manager.service.UpmsFileNumService;
import com.waypolice.mapper.CmsSinItemMapper;
import com.waypolice.mapper.UpmsDictionaryMapper;
import com.waypolice.pojo.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description
 * @Author wangrang
 * @Date Created in 15:52 2019/3/25
 * @Modified By:
 */
@Service
public class CmsSinItemServiceImpl implements CmsSinItemService {

    @Autowired
    private CmsSinItemMapper cmsSinItemMapper;

    @Autowired
    private UpmsFileNumService upmsFileNumService;

    @Autowired
    private UpmsDictionaryMapper upmsDictionaryMapper;

    @Override
    public List<CmsSinItem> selectAllByQueryParms(CmsSinItemExample cmsSinItemExample) {
        return cmsSinItemMapper.selectByExample(cmsSinItemExample);
    }

    @Override
    public String selectByPojo(Long itemId) {
        CmsSinItemExample cmsSinItemExample = new CmsSinItemExample();
        CmsSinItemExample.Criteria criteria = cmsSinItemExample.createCriteria();
        criteria.andItemIdEqualTo(itemId);
        criteria.andItemStr3EqualTo("1");
        List<CmsSinItem> cmsSinItems = cmsSinItemMapper.selectByExample(cmsSinItemExample);
        if(cmsSinItems.size()==0 || cmsSinItems.get(0)==null){
            return null;
        }
        String itemStr = cmsSinItems.get(0).getItemStr2();
        if(StringUtils.isNotBlank(itemStr)) {
            return itemStr;
        }else {
            return null;
        }
    }
	
    @Override
    public String getSingItemFileNumber(CmsSinItem cmsSinItem) throws Exception {
        //获取档号规则
        UpmsFileNumExample fileNumExample = new UpmsFileNumExample();
        fileNumExample.setOrderByClause("SORT_NUM asc");
        fileNumExample.or().andStr1EqualTo("1").andPkCodeEqualTo("singleArchive");
        List<UpmsFileNum> fileNumsLis = upmsFileNumService.selectByExample(fileNumExample);
        StringBuffer strSingItemFileNum = new StringBuffer();
        for (int i = 0; i < fileNumsLis.size(); i++) {
            if("itemFixedValue".equals(fileNumsLis.get(i).getCode())){
                UpmsDictionaryExample upmsDictionaryExample = new UpmsDictionaryExample();
                UpmsDictionaryExample.Criteria criteria = upmsDictionaryExample.createCriteria();
                criteria.andCodeEqualTo("itemFixedValue");
                List<UpmsDictionary> upmsDictionaries = upmsDictionaryMapper.selectByExample(upmsDictionaryExample);
                String remarks = upmsDictionaries.get(0).getRemarks();
                strSingItemFileNum.append(remarks);
            }else {
                String getMethod = getGetMethod(cmsSinItem, fileNumsLis.get(i).getCode()) == null ? null : getGetMethod(cmsSinItem, fileNumsLis.get(i).getCode()).toString();
                if(StringUtils.isBlank(getMethod)){
                    return "error";
                }else {
                    strSingItemFileNum.append(getMethod);
                }
            }
            if(i != fileNumsLis.size()-1){
                strSingItemFileNum.append(fileNumsLis.get(i).getSeparator());
            }

        }
        return strSingItemFileNum.toString();
    }

    /**
     * 根据属性，获取get方法
     * @param ob 对象
     * @param name 属性名
     * @return
     * @throws Exception
     */
    public static Object getGetMethod(Object ob , String name)throws Exception {
        Method[] m = ob.getClass().getMethods();
        for (int i = 0; i < m.length; i++) {
            if (("get" + name).toLowerCase().equals(m[i].getName().toLowerCase())) {
                return m[i].invoke(ob);
            }
        }
        return null;
    }

    /**
     * @author:zyp
     * @description 根据id获取单件数据
     * @date: 2019/3/27 9:59
     * @return 
     */
    @Override
    public List<CmsSinItem> selectSimItemsByIds(List<String> ids) {
        List<Long> longIds = new ArrayList<>();
        for(String id : ids ){
            longIds.add(MathUtils.string2long(id));
        }
        CmsSinItemExample cmsSinItemExample = new CmsSinItemExample();
        CmsSinItemExample.Criteria criteria = cmsSinItemExample.createCriteria();
        criteria.andItemIdIn(longIds);
        List<CmsSinItem> cmsSinItems = cmsSinItemMapper.selectByExample(cmsSinItemExample);
        return cmsSinItems;
    }

    /**
     * @author:zyp
     * @description 根据id修改单件数据
     * @date: 2019/3/27 10:10
     * @return
     */
    @Override
    public void updateSinItemByPrimaryKeySelective(CmsSinItem cmsSinItem) {
        cmsSinItemMapper.updateByPrimaryKeySelective(cmsSinItem);
    }

    /**
     * @author:zyp
     * @description 根据单件id获取单件数据
     * @date: 2019/3/27 11:10
     * @return 
     */
    @Override
    public CmsSinItem selectSinItemById(Long fileId) {
        CmsSinItem cmsSinItem = cmsSinItemMapper.selectByPrimaryKey(fileId);
        return cmsSinItem;
    }
}
